<?php

namespace app\business\controller;

use think\Controller;
use Util\SysDb;
use think\Db;
class Goodinfo extends Controller
{
    public function getstore()
    {
        $this->db = new SysDb();
        $admin = $this->db->table('goods_store')->where(['status' => 1])->lists();
        if (!$admin) {
            exit(json_encode(['code' => 1, 'message' => 'error', "result" => "无仓库"]));
        } else {
            exit(json_encode(['message' => 'succ', 'result' => $admin]));
        }
    }

    public function goodstypes()
    {
        $this->db = new SysDb();
        $admin = $this->db->table('goods_type')->lists();
        if (!$admin) {
            exit(json_encode(['code' => 1, 'message' => 'error', "result" => "无仓库"]));
        } else {
            exit(json_encode(['message' => 'succ', 'result' => $admin]));
        }
    }
    public function list()
    {
        // ->where(['store_id' => $storeId])->lists()
        $storeId = trim(input('param.id'));
        $tenantid = trim(input('param.tenantid'));
        // $this->db = new SysDb();
        // $admin = $this->db->table('goods_info')->join('as gi left join goods_store as gs on gi.store_id = gs.id left join tenant_goods_price as tgp on gi.id = tgp.gooods_id')->where(['store_id' => $storeId])->lists();
        $admin= Db::table('goods_info')
            ->alias(' gi')
            ->join('goods_store gs', 'gi.store_id = gs.id',"left")
            ->join('tenant_goods_price tgp', 'gi.id = tgp.goods_id',"left")
            ->where(['store_id' => $storeId, "tenant_id"=>$tenantid])
            ->field(array(' gi.store_id' =>'storeId','gi.types'=>'types','gi.id'=>"giId",'gi.status'=>"giStatus",'gi.cover','gi.name','gi.sales_num'=>'salesNum','gi.descr','gi.weight','gi.cost','tgp.id'=>'id','tgp.profit'=>'profit','gi.repertory'=>"repertory"))
            ->select();

        if (!$admin) {
            exit(json_encode(['code' => 1, 'message' => 'error', "result" => "无库"]));
        } else {
            exit(json_encode(['message' => 'succ', 'result' => $admin]));
        }
    }
    public function goodlist(){
         // ->where(['store_id' => $storeId])->lists()
         $storeId = trim(input('param.sort'));
         $tenantid = trim(input('param.tenantid'));
         $page = trim(input('param.pageNo'));
         $limit= trim(input('param.pageSize'));
         $tol = (intval($page) - 1) * $limit;
         // $this->db = new SysDb();
         // $admin = $this->db->table('goods_info')->join('as gi left join goods_store as gs on gi.store_id = gs.id left join tenant_goods_price as tgp on gi.id = tgp.gooods_id')->where(['store_id' => $storeId])->lists();
         $admin= Db::table('goods_info')
             ->alias(' gi')
             ->join('goods_store gs', 'gi.store_id = gs.id',"left")
             ->join('tenant_goods_price tgp', 'gi.id = tgp.goods_id',"left")
             ->where(['sort' => $storeId ,"tenant_id"=>$tenantid])
             ->limit($tol, $limit)
             ->field(array(' gi.store_id' =>'storeId','gi.types'=>'types','gi.id'=>"giId",'gi.status'=>"giStatus",'gi.cover','gi.name','gi.sales_num'=>'salesNum','gi.descr','gi.weight','gi.cost','tgp.id'=>'id','tgp.profit'=>'profit','gi.repertory'=>"repertory"))
             ->select();
        
         if (!$admin) {
             exit(json_encode(['code' => 1, 'message' => 'error', "result" => "无商品"]));
         } else {
            $admins= Db::table('goods_info')
            ->alias(' gi')
            ->join('goods_store gs', 'gi.store_id = gs.id',"left")
            ->join('tenant_goods_price tgp', 'gi.id = tgp.goods_id',"left")
            ->where(['sort' => $storeId , "tenant_id"=>$tenantid])
            ->field(array(' gi.store_id' =>'storeId','gi.types'=>'types','gi.id'=>"giId",'gi.status'=>"giStatus",'gi.cover','gi.name','gi.sales_num'=>'salesNum','gi.descr','gi.weight','gi.cost','tgp.id'=>'id','tgp.profit'=>'profit','gi.repertory'=>"repertory"))
            ->select();
             $len=count($admins);
             $list=array();
             array_push($list,$admin);
             array_push($list,$len);
             exit(json_encode(['message' => 'succ', 'result' => $list]));
         }
    }
    public function goodlists(){
        // ->where(['store_id' => $storeId])->lists()
        $storeId = trim(input('param.storeId'));
        $tenantid = trim(input('param.tenantid'));
        $sort = trim(input('param.sort'));
        $page = trim(input('param.pageNo'));
        $limit= trim(input('param.pageSize'));
        $tol = (intval($page) - 1) * $limit;
        // $this->db = new SysDb();
        // $admin = $this->db->table('goods_info')->join('as gi left join goods_store as gs on gi.store_id = gs.id left join tenant_goods_price as tgp on gi.id = tgp.gooods_id')->where(['store_id' => $storeId])->lists();
       if($storeId!=""){
        $admin= Db::table('goods_info')
            ->alias('gi')
            ->join('goods_store gs', 'gi.store_id = gs.id',"left")
            ->join('tenant_goods_price tgp', 'gi.id = tgp.goods_id',"left")
            ->where(['store_id' => $storeId,"tenant_id"=>$tenantid])
            ->limit($tol, $limit)
            ->field(array(' gi.store_id' =>'storeId','gi.types'=>'types','gi.id'=>"giId",'gi.status'=>"giStatus",'gi.cover','gi.name','gi.sales_num'=>'salesNum','gi.descr','gi.weight','gi.cost','tgp.id'=>'id','tgp.profit'=>'profit','gi.repertory'=>"repertory"))
            ->select();
            $admins= Db::table('goods_info')
            ->alias(' gi')
            ->join('goods_store gs', 'gi.store_id = gs.id',"left")
            ->join('tenant_goods_price tgp', 'gi.id = tgp.goods_id',"left")
            ->where(['store_id' => $storeId,"tenant_id"=>$tenantid])
            ->field(array(' gi.store_id' =>'storeId','gi.types'=>'types','gi.id'=>"giId",'gi.status'=>"giStatus",'gi.cover','gi.name','gi.sales_num'=>'salesNum','gi.descr','gi.weight','gi.cost','tgp.id'=>'id','tgp.profit'=>'profit','gi.repertory'=>"repertory"))
            ->select();
            $len=count($admins);
        }else{
            $admin= Db::table('goods_info')
            ->alias(' gi')
            ->join('goods_store gs', 'gi.store_id = gs.id',"left")
            ->join('tenant_goods_price tgp', 'gi.id = tgp.goods_id',"left")
            ->where(["tenant_id"=>$tenantid])
            ->limit($tol, $limit)
            ->field(array(' gi.store_id' =>'storeId','gi.types'=>'types','gi.id'=>"giId",'gi.status'=>"giStatus",'gi.cover','gi.name','gi.sales_num'=>'salesNum','gi.descr','gi.weight','gi.cost','tgp.id'=>'id','tgp.profit'=>'profit','gi.repertory'=>"repertory"))
            ->select();
            $admins= Db::table('goods_info')
            ->alias(' gi')
            ->join('goods_store gs', 'gi.store_id = gs.id',"left")
            ->join('tenant_goods_price tgp', 'gi.id = tgp.goods_id',"left")
            ->where(["tenant_id"=>$tenantid])
            ->field(array(' gi.store_id' =>'storeId','gi.types'=>'types','gi.id'=>"giId",'gi.status'=>"giStatus",'gi.cover','gi.name','gi.sales_num'=>'salesNum','gi.descr','gi.weight','gi.cost','tgp.id'=>'id','tgp.profit'=>'profit','gi.repertory'=>"repertory"))
            ->select();
            $len=count($admins);
        }
        if (!$admin) {
            exit(json_encode(['code' => 1, 'message' => 'error', "result" => "无商品"]));
        } else {
            $list=array();
            array_push($list,$admin);
            array_push($list,$len);
            exit(json_encode(['message' => 'succ', 'result' => $list]));
        }
   }
    public function newdetail()
    {
        // ->where(['store_id' => $storeId])->lists()
        $storeId = trim(input('param.goodsId'));
        // $this->db = new SysDb();
        // $admin = $this->db->table('goods_info')->join('as gi left join goods_store as gs on gi.store_id = gs.id left join tenant_goods_price as tgp on gi.id = tgp.gooods_id')->where(['store_id' => $storeId])->lists();
        $admin= Db::table('goods_info')
            ->alias(' gi')
            ->join('goods_store gs', 'gi.store_id = gs.id',"left")
            ->join('tenant_goods_price tgp', 'gi.id = tgp.goods_id',"left")
            ->where(['goods_id' => $storeId])
            ->field(array(' gi.store_id' =>'storeId','gi.types'=>'types','gi.id'=>"giId",'gi.status'=>"giStatus",'gi.cover','gi.name','gi.sales_num'=>'salesNum','gi.descr','gi.weight','gi.cost','tgp.id'=>'id','tgp.profit'=>'profit','gi.repertory'=>"repertory"))
            ->select();

        if (!$admin) {
            $admins= Db::table('goods_info')
            ->alias(' gi')
            ->join('goods_store gs', 'gi.store_id = gs.id',"left")
            ->join('tenant_goods_price tgp', 'gi.id = tgp.goods_id',"left")
            ->where(['goods_id' => 105])
            ->field(array(' gi.store_id' =>'storeId','gi.types'=>'types','gi.id'=>"giId",'gi.status'=>"giStatus",'gi.cover','gi.name','gi.sales_num'=>'salesNum','gi.descr','gi.weight','gi.cost','tgp.id'=>'id','tgp.profit'=>'profit','gi.repertory'=>"repertory"))
            ->select();

            exit(json_encode(['message' => 'succ', 'result' => $admins[0]]));
        } else {
            exit(json_encode(['message' => 'succ', 'result' => $admin]));
        }
    }
    public function detail()
    {
       $id = trim(input('param.id'));
       $this->db = new SysDb();
       $good = $this->db->table('goods_info')->where(['id' => $id])->item();
       $goodstroe=$this->db->table('goods_store')->where(['id' =>  $good['store_id']])->item();
       $data=array();
       array_push($data,$good);
       array_push($data,$goodstroe);
       exit(json_encode(['message' => 'succ', 'result' =>$data]));
    }
    public function getInviteCode(){
        $id = trim(input('param.id'));
        $encode = base64_encode($id );
        exit(json_encode(['message' => 'succ', 'result' =>$encode]));
    }
    public function levelList(){
        $tenantId = trim(input('param.tenantId'));
        $this->db = new SysDb();
        $list = $this->db->table('tenant_vip_level')->where(['tenant_id' => $tenantId])->where(['condition_num' => 0])->lists();
        $lista = $this->db->table('tenant_vip_level')->where(['tenant_id' => $tenantId])->where(['condition_num' => 3])->lists();
        $lists=array();
        array_push($lists,$list[0]);
        array_push($lists,$lista[0]);
         exit(json_encode(['message' => 'succ', 'result' => $lists]));
    }
    public function spreadList(){
        $id = trim(input('param.id'));
        $page = trim(input('param.pageNo'));
        $limit= trim(input('param.pageSize'));
        $tol = (intval($page) - 1) * $limit;
        $list =DB::name("bus_info")->order("create_time desc")->where('parent_id', $id)->limit($tol, $limit)->select();
        $this->db = new SysDb();
        $count = $this->db->table('bus_info')->where(['parent_id' => $id]) ->count();
        $data=array();
        array_push($data,$list);
        array_push($data,$count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
        // $id = trim(input('param.id'));
        // $this->db = new SysDb();
        // $list = $this->db->table('bus_info')->where(['parent_id' => $id])->lists();
        // exit(json_encode(['message' => 'succ', 'result' => $list]));
    }

}
